/**
 * @param {string} s
 * @param {number} numRows
 * @return {string}
 */
var convert = function (s, numRows) {
  let str = "";
  let length = s.length;
  let arr1 = 2 * (numRows - 1);
  let max = Math.ceil(length / arr1);
  let arr_one = [];

  if (length < numRows || length === 1 || numRows === 1) {
    return s;
  }

  for (let index = 0; index < max; index++) {
    if (s.charAt(index * arr1)) {
      str += s.charAt(index * arr1);
      arr_one.push(index * arr1);
    }
  }

  arr_one.push(arr_one[arr_one.length - 1] + arr1);

  let length_one = arr_one.length;
  let result = 1; // 层数

  const deepMath = () => {
    let str_deep = "";
    if (result === numRows) {
      return str;
    }

    for (let i = 0; i < length_one; i++) {
      if (result === numRows - 1) {
        // 最后一层
        if (s.charAt(arr_one[i] + result)) {
          str_deep += s.charAt(arr_one[i] + result);
        }
      } else {
        // 第二层到倒数第二层
        if (s.charAt(arr_one[i] - result)) {
          str_deep += s.charAt(arr_one[i] - result);
        }

        if (s.charAt(arr_one[i] + result)) {
          str_deep += s.charAt(arr_one[i] + result);
        }
      }
    }

    str += str_deep;
    result += 1;
    // console.log(str_deep);
    deepMath();
  };

  deepMath();

  return str;
};

let str = "ABCD";
let numRows = 3;
console.log(convert(str, numRows));
